﻿using System;
using System.Text;
using System.Xml;

namespace Meqs.Entity.Views
{
    [Serializable]
    public class ItemsXml
    {
        private int _id;
        private double _score;
        private double _max;
        private string _reply;
        private string _opt;

        public ItemsXml()
        {
        }

        public ItemsXml(int id, double score, double max, string reply, string opt)
        {
            _id = id;
            _score = score;
            _max = max;
            _reply = reply;
            _opt = opt;
        }

        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        public double Score
        {
            get { return _score; }
            set { _score = value; }
        }

        public double Max
        {
            get { return _max; }
            set { _max = value; }
        }

        public string Reply
        {
            get { return _reply; }
            set { _reply = value; }
        }

        public string Opt
        {
            get { return _opt; }
            set { _opt = value; }
        }

        /// <summary>
        /// ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<item id=\"{0}\">", _id);
            sb.AppendFormat("<score>{0}</score>", _score);
            sb.AppendFormat("<max>{0}</max>", _max);
            if (_reply == null)
                sb.Append("<reply />");
            else
                sb.AppendFormat("<reply>{0}</reply>", _reply);
            if (_opt == null)
                sb.Append("<opt />");
            else
                sb.AppendFormat("<opt>{0}</opt>", _opt);
            sb.Append("</item>");
            return sb.ToString();
        }

        /// <summary>
        /// 解析xml
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public ItemsXml Deserialize(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            XmlNode node = doc.SelectSingleNode("item");
            if (node == null) return null;
            int id = Convert.ToInt32(node.Attributes["id"].Value);
            XmlNode child = node.FirstChild;
            double score = Convert.ToDouble(child.Value);
            child = child.NextSibling;
            double max = Convert.ToDouble(child.NextSibling.Value);
            child = child.NextSibling;
            string reply = child.Value;
            child = child.NextSibling;
            string opt = child.Value;
            
            return new ItemsXml(id, score, max, reply, opt);
        }

        public bool Equals(ItemsXml other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other._id == _id;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (ItemsXml)) return false;
            return Equals((ItemsXml) obj);
        }

        public override int GetHashCode()
        {
            return _id;
        }
    }
}
